package com.ysh.smartplatform.controller;

import com.ysh.smartplatform.dto.R;
import com.ysh.smartplatform.dto.UserDto;
import com.ysh.smartplatform.pojo.SysUserInfo;
import com.ysh.smartplatform.service.OnLineUserService;
import com.ysh.smartplatform.service.UserService;
import com.ysh.smartplatform.valigroup.Insert;
import com.ysh.smartplatform.valigroup.Select;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 袁胜虎 on 2022/9/5 16:44
 */
@RestController
@RequestMapping("/users")
@Api(tags = "登录管理模块")
public class UserController {

    private OnLineUserService onLineUserService;

    @Autowired
    public void setOnLineUserService(OnLineUserService onLineUserService) {
        this.onLineUserService = onLineUserService;
    }

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    /**
     * 注册接口
     *
     * @param userDto 接收用户传递的参数的对象
     * @return
     */
    @PostMapping("/user")
    @ApiOperation("注册用户")
    public R register(@Validated(Insert.class)  @RequestBody UserDto userDto){
        System.err.println(userDto);
        userService.addUser(userDto);
        return R.OK();
    }

    @PostMapping("/login")
    @ApiOperation("登录")
    public R login(@RequestBody @Validated(Select.class) UserDto userDto, HttpSession session){
        SysUserInfo sysUserInfo = userService.login(userDto.getUsername(), userDto.getPassword());
        session.setAttribute("user",sysUserInfo);
        return R.OK();
    }

//    根据登录用户名删除对应的登录记录
    @DeleteMapping("/online/{loginName}")
    @ApiOperation("删除登录记录")
    public R deleteOnline( @PathVariable  String loginName){
        onLineUserService.deleteOnline(loginName);
        return R.OK();
    }
    //根据id删除用户
    @DeleteMapping("/user/{id}")
    @ApiOperation("根据id删除用户")
    public R deleteById(  @PathVariable String id){
        userService.deleteById(id);
        return R.OK();
    }
}
